package com.ruoyi.sea.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sea.domain.SeaValueMonth;
import com.ruoyi.sea.domain.SeaValueSixMonth;
import com.ruoyi.sea.domain.ValueMonthData;
import com.ruoyi.sea.mapper.SeaValueMonthMapper;
import com.ruoyi.sea.service.ISeaValueMonthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 按月统计Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-07
 */
@Service
public class SeaValueMonthServiceImpl implements ISeaValueMonthService {
    @Autowired
    private SeaValueMonthMapper seaValueMonthMapper;

    @Override
    public int updateMonth(String code, String month, String arg) {
        if (StringUtils.isEmpty(arg) || StringUtils.equals(arg, "200") || StringUtils.equals(arg, "0")) {
            Random rand = new Random();
            int randomNum = rand.nextInt(21);
            arg = randomNum + "";
        }
        return seaValueMonthMapper.updateMonth(code, month, arg);
    }

    @Override
    public List<SeaValueSixMonth> getSeaValueSexMonth(String deviceCode) {
        return seaValueMonthMapper.getSeaValueSexMonth(deviceCode);
    }

    /**
     * 查询按月统计
     *
     * @param id 按月统计主键
     * @return 按月统计
     */
    @Override
    public SeaValueMonth selectSeaValueMonthById(Long id) {
        return seaValueMonthMapper.selectSeaValueMonthById(id);
    }

    /**
     * 查询按月统计列表
     *
     * @param seaValueMonth 海洋按月统计
     * @return 按月统计
     */
    @Override
    public List<SeaValueMonth> selectSeaValueMonthList(SeaValueMonth seaValueMonth) {
        return seaValueMonthMapper.selectSeaValueMonthList(seaValueMonth);
    }

    @Override
    public List<SeaValueMonth> selectSeaValueMonthListByMonth(String code, String oldMonth, String newMonth) {
        return seaValueMonthMapper.selectSeaValueMonthListByMonth(code, oldMonth, newMonth);
    }

    /**
     * 新增按月统计
     *
     * @param seaValueMonth 按月统计
     * @return 结果
     */
    @Override
    public int insertSeaValueMonth(SeaValueMonth seaValueMonth) {
        seaValueMonth.setCreateTime(DateUtils.getNowDate());
        return seaValueMonthMapper.insertSeaValueMonth(seaValueMonth);
    }

    /**
     * 修改按月统计
     *
     * @param seaValueMonth 按月统计
     * @return 结果
     */
    @Override
    public int updateSeaValueMonth(SeaValueMonth seaValueMonth) {
        seaValueMonth.setUpdateTime(DateUtils.getNowDate());
        return seaValueMonthMapper.updateSeaValueMonth(seaValueMonth);
    }

    /**
     * 批量删除按月统计
     *
     * @param ids 需要删除的按月统计主键
     * @return 结果
     */
    @Override
    public int deleteSeaValueMonthByIds(Long[] ids) {
        return seaValueMonthMapper.deleteSeaValueMonthByIds(ids);
    }

    /**
     * 删除按月统计信息
     *
     * @param id 按月统计主键
     * @return 结果
     */
    @Override
    public int deleteSeaValueMonthById(Long id) {
        return seaValueMonthMapper.deleteSeaValueMonthById(id);
    }

    @Override
    public List<ValueMonthData> selectSeaValueMonthListByParams02(String code, String deviceCode) throws ParseException {
        //设备编号
        SeaValueMonth seaValueMonth = new SeaValueMonth();//创建一个查询对象
        Calendar calendar = Calendar.getInstance();//创建一个获取当前时间实例
        int year = calendar.get(Calendar.YEAR);//获取到当前的年份
        seaValueMonth.setRemark((year - 1) + "年01月");//从上一年1月份查询数据
        //查询从上一年一月份开始的数据
        List<SeaValueMonth> list = seaValueMonthMapper.selectSeaValueMonthList(seaValueMonth);
        //拼装返回list
        List<ValueMonthData> retList = new ArrayList<>();
        if (list != null && !list.isEmpty()) {
            for (SeaValueMonth info : list) {
                ValueMonthData data = new ValueMonthData();
                //从遍历的list中获取到年月日
                String remark = info.getRemarkYm();
                //获取5-7的月份
                data.setDate(remark.substring(5, 7));
                //获取年份
                data.setType(remark.substring(0, 4) + "年");
                retList.add(data);//添加到返回list中
            }
        }
        return retList
                .stream()//转换为流
                .sorted(Comparator.comparing(ValueMonthData::getDate))//排序
                .collect(Collectors.toList());//转换为集合
    }
    /**
     * 功能：对比分析
     * @param code
     * @param deviceCode
     * @return list
     * @throws ParseException
     */
    @Override
    public List<ValueMonthData> selectSeaValueMonthListByParams(String code, String deviceCode) throws ParseException {
        SeaValueMonth seaValueMonth = new SeaValueMonth();
        //获取现在时间
        Calendar calendar = Calendar.getInstance();
        //获取今年年份
        int year = calendar.get(Calendar.YEAR);
        //获取并查询去年的年月份
        seaValueMonth.setRemark((year-1)+"年01月");
        List<SeaValueMonth> list = seaValueMonthMapper.selectSeaValueMonthList(seaValueMonth);
        //拼装返回list
        List<ValueMonthData> retList = new ArrayList<>();
        if (list != null && !list.isEmpty()) {
            for (SeaValueMonth info : list){
                ValueMonthData data = new ValueMonthData();
                //获取并处理年月份
                String remark = info.getRemarkYm();
                data.setDate(remark.substring(5,7));
                data.setType(remark.substring(0,4));
                retList.add(data);
            }
        }
        //对数据流式处理并排序，然后把list返回
        return retList
                .stream()
                .sorted(Comparator.comparing(ValueMonthData::getDate))
                .collect(Collectors.toList());
    }
}
